package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.GroupScoresMapper;
import com.group3.dao.PersonalScoresMapper;
import com.group3.domain.*;
import com.group3.pojo.Page;
import com.group3.pojo.result.GroupScoresResult;
import com.group3.pojo.result.PersonalScoresResult;
import com.group3.service.ACompositeScoresService;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ACompositeScoresServiceImpl implements ACompositeScoresService {

    @Resource
    private PersonalScoresMapper personalScoresMapper;
    @Resource
    private GroupScoresMapper groupScoresMapper;

    @Override
    @Transactional
    public Result<List<PersonalScores>> selectCompositeScoresPersonal(String lessonName, Integer groupId, Page page) {

        PersonalScoresExample example = new PersonalScoresExample();

        PersonalScoresExample.Criteria criteria = example.createCriteria();

        if (NotNull.judge(lessonName)) {
            criteria.andLessonNameEqualTo(lessonName);
        }
        if (NotNull.judge(groupId)) {
            criteria.andGroupIdEqualTo((long) groupId);
        }
        example.setOrderByClause("student_id");
//        1.先开启分页
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());//启动分页
//        2.执行SQL语句
        List<PersonalScores> personalScores1 = personalScoresMapper.selectByExample(example);
//        3.获取分页信息
        PageInfo<PersonalScores> personalScoresPageInfo = new PageInfo<>(personalScores1);//获取分页信息

        return ResultUtil.success("查询成功", personalScores1, personalScoresPageInfo.getTotal());
    }

    @Override
    @Transactional
    public Result<PersonalScoresResult> showStudentScore(String studentId, String lessonName) {

        PersonalScoresExample example = new PersonalScoresExample();
        PersonalScoresExample.Criteria criteria = example.createCriteria();

        if (NotNull.judge(lessonName) || NotNull.judge(studentId)) {
            ResultUtil.error("动歪心思？");
        }

        if (NotNull.judge(lessonName)) {
            criteria.andLessonNameEqualTo(lessonName);
        }
        if (NotNull.judge(studentId)) {
            criteria.andStudentIdEqualTo(studentId);
        }

        List<PersonalScoresResult> personalScoresDetails = personalScoresMapper.selectByExample1(example);

        return ResultUtil.success("查询成功", personalScoresDetails);
    }

    @Override
    @Transactional
    public Result<List<GroupScores>> selectCompositeScoresGroup(String lessonName, Integer groupId, Page page) {

//        personalScores.s
        GroupScoresExample example = new GroupScoresExample();

        GroupScoresExample.Criteria criteria = example.createCriteria();

        if (lessonName != null) {
            criteria.andLessonNameEqualTo(lessonName);
        }
        if (groupId != null) {
            criteria.andGroupIdEqualTo(groupId);
        }

//        1.先开启分页
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());//启动分页
//        2.执行SQL语句
        List<GroupScores> groupScores = groupScoresMapper.selectByExample(example);
//        3.获取分页信息
        PageInfo<GroupScores> personalScoresPageInfo = new PageInfo<>(groupScores);//获取分页信息

        return ResultUtil.success("查询成功", groupScores, personalScoresPageInfo.getTotal());
    }

    @Override
    @Transactional
    public Result showGroupScore(Integer groupId, String lessonName) {


        GroupScoresExample example = new GroupScoresExample();

        example.createCriteria()
                .andLessonNameEqualTo(lessonName)
                .andGroupIdEqualTo(groupId);


        List<GroupScoresResult> groupScores = groupScoresMapper.selectByExample1(example);

        return ResultUtil.success("查询成功", groupScores);
    }
}
